package com.boot.main.controller;

import com.boot.main.utils.FileUtil;
import com.boot.main.entity.CcomPermission;
import com.boot.main.mapper.CcomPermissionMapper;
import com.boot.main.response.ResponseDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/v1/api")
@Api(tags = "文件模块")
public class FileController {
    @Resource
    private CcomPermissionMapper ccomPermissionMapper;

    @RequestMapping(value="/uploadImg", method = RequestMethod.POST)
    @ApiOperation(value = "图片上传")
    public Object uplodadImg(MultipartFile file, HttpServletRequest request){
        String filePath = FileUtil.uploadFile(file, request);
        Map map = new HashMap();
        map.put("filePath", filePath);
        return ResponseDataUtil.success("上传文件成功", map);
    }

    @SneakyThrows
    @RequestMapping(value="/exportExcel", method = RequestMethod.GET)
    @ApiOperation(value="导出Excel数据")
    public void exportPermissionExcel(HttpServletResponse response){
        List<CcomPermission> permissions = ccomPermissionMapper.getPermissions(0, 1000, "");
        FileUtil.exportExcel(response, "权限列表", permissions, CcomPermission.class);
//        return ResponseDataUtil.success("导出文件成功", null);
    }

    @SneakyThrows
    @RequestMapping(value="/importExcel", method = RequestMethod.POST)
    @ApiOperation(value = "导入Excel数据")
    public Object importData(MultipartFile file) {
        List<CcomPermission> permissions = FileUtil.importExcel(file, CcomPermission.class);
        ccomPermissionMapper.savePermissions(permissions);
        return ResponseDataUtil.success("导入文件成功", null);
    }
}
